//package com.wbk.lotterysystemgateway.filter;
//
//import com.auth0.jwt.interfaces.DecodedJWT;
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.coolgua.common.bean.ApiCode;
//import com.coolgua.common.bean.ApiResult;
//import com.coolgua.common.constant.SessionConstant;
//import com.coolgua.common.util.JWTUtil;
//import com.coolgua.models.entity.User;
//import com.coolgua.service.UserService;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//import org.springframework.web.servlet.HandlerInterceptor;
//import org.springframework.web.servlet.ModelAndView;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//
///*******************************************************************************
// * @project: rate
// * @package: com.coolgua.config.interceptor
// * @author: wbk
// * @created: 2021/4/28
// * @purpose:
// ******************************************************************************/
//@Component
//@Slf4j
//public class ApiInterceptor implements HandlerInterceptor {
//
//    @Autowired
//    UserService userService;
//
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        if("OPTIONS".equals(request.getMethod())){
//            return true;
//        }
//        String token = request.getHeader("Authorization");
//        DecodedJWT decodedJWT;
//        User user;
//        try {
//            decodedJWT = JWTUtil.verify(token);
//            String userNo = JWTUtil.getUserNo(decodedJWT);
//            QueryWrapper<User> qw = new QueryWrapper<>();
//            qw.eq("user_no", userNo).last("limit 1");
//            user = userService.getOne(qw);
//            if (user == null) {
//                HttpResponseUtil.send(response, ApiResult.fail(ApiCode.HTTP_UNAUTHORIZED));
//                return false;
//            }
//            // 将用户信息放入到request中
//            request.setAttribute(SessionConstant.USER_SESSION, user);
//        } catch (Exception e) {
//            log.error(e.getMessage());
//            HttpResponseUtil.send(response, ApiResult.fail(ApiCode.HTTP_UNAUTHORIZED));
//            return false;
//        }
//        return true;
//    }
//
//    @Override
//    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
//    }
//
//    @Override
//    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
//    }
//}
